<style>
    .rating{{ $id }} {
        font-size: 16px;
        cursor: pointer;
    }
    .star{{ $id }} {
        color: #ddd;
        transition: color 0.3s;
    }
    .star{{ $id }}.active {
        color: #7f5006;
    }
    #result{{ $id }} {
        color: #666;
    }
</style>
<div class="rating{{ $id }}">
    <span class="star{{ $id }}" data-value="1">☆</span>
    <span class="star{{ $id }}" data-value="2">☆</span>
    <span class="star{{ $id }}" data-value="3">☆</span>
    <span class="star{{ $id }}" data-value="4">☆</span>
    <span class="star{{ $id }}" data-value="5">☆</span>
</div>
<script>
    $(document).ready(function() {
        let currentRating = 0;
        const stars = $('.star{{ $id }}');

        // 初始化加载评分
        $.ajax({
            url: '/admin/ajax/getScore',
            data: {id: {{ $id }}},
            success: function(response) {
                currentRating = response.data.score;
                updateStars(currentRating);
            }
        });

        // 点击评分
        stars.on('click', function() {
            currentRating = parseInt($(this).data('value'));
            submitRating(currentRating);
        });

        function updateStars(rating) {
            stars.each(function() {
                $(this).toggleClass('active', $(this).data('value') <= rating);
            });
        }

        function submitRating(rating) {
            $.ajax({
                url: '/admin/ajax/saveScore',
                method: 'POST',
                data: {
                    id: {{ $id }},
                    score: rating,
                    _token: $.admin.token
                },
                success: function() {
                    alert('客户{{ $name }}的星级已调整为'+rating+'颗星');
                    updateStars(rating);
                },
                error: function() {
                    alert('客户{{ $name }}的星级调整失败，已重置');
                    currentRating = 0; // 失败时重置评分
                    updateStars(0);
                }
            });
        }
    });
</script>
